package com.leetcode.algs4.graph.mst;

/**
 * @author Dennis Li
 * @date 2020/6/18 18:56
 */
public class Edge implements Comparable<Edge> {

    // 边的两个顶点
    private final int v;
    private final int w;
    private final double weight; // 边的权重

    public Edge(int v, int w, double weight) {
        this.v = v;
        this.w = w;
        this.weight = weight;
    }

    public double weight() {
        return weight;
    }

    public int either() {
        return v;
    }

    // 已知一个顶点v，求边的另一个顶点
    public int other(int vertex) {
        if (vertex == v) return w;
        else if (vertex == w) return v;
        else throw new RuntimeException("Inconsistent edge");
    }

    @Override
    public int compareTo(Edge that) {
        return Double.compare(this.weight(), that.weight());
    }

    @Override
    public String toString() {
        return "Edge{" + "v=" + v + ", w=" + w + ", weight=" + weight + '}';
    }
}
